package com.yt.system.controller;

import com.yt.system.domain.GroupAccount;
import com.yt.system.service.GroupAccountService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@Api(tags = "小组账户管理")
@RestController
public class GroupAccountController {
    @Autowired
    GroupAccountService groupAccountService;

    /**
     * 查询所有小组账户信息，按积分降序排序
     * @return
     */
    @ApiOperation(value = "查询所有小组账户信息，按积分降序排序")
    @GetMapping("/selectAllGroupAccountDesc")
    public List<GroupAccount> selectAllGroupAccountDesc(){
        return groupAccountService.selectAllGroupAccountDesc();
    }

    /**
     * 根据小组id查询小组账户信息
     * @param gid
     * @return
     */
    @ApiOperation(value = "根据小组id查询小组账户信息")
    @GetMapping("/selectGroupAccountByGid/{gid}")
    public GroupAccount selectGroupAccountByGid(@PathVariable("gid") Integer gid){
        return groupAccountService.selectByGid(gid);
    }

    /**
     * 添加小组账户信息
     * @param groupAccount
     * @return
     */
    @ApiOperation(value = "添加小组账户信息")
    @PostMapping("/addGroupAccount")
    public String addGroupAccount(@RequestBody GroupAccount groupAccount){

        try{
            groupAccountService.addGroupAccount(groupAccount);
            return "success";
        }catch (Exception e){
            e.printStackTrace();
            return "error";
        }
    }

    /**
     * 根据小组id重置小组账户积分
     * @param gid
     * @return
     */
    @ApiOperation(value = "根据小组id重置小组账户积分")
    @PutMapping("/resetGroupScoreByGid/{gid}")
    public String resetGroupScoreByGid(@PathVariable("gid") Integer gid){
        try{
            groupAccountService.resetGroupScoreByGid(gid);
            return "success";
        }catch (Exception e){
            e.printStackTrace();
            return "error";
        }
    }

    /**
     * 重置所有小组的账户积分
     * @return
     */
    @ApiOperation(value = "重置所有小组的账户积分")
    @PutMapping("/resetAll")
    public String resetAll(){
        List<GroupAccount> groupAccounts = groupAccountService.selectAllGroupAccountDesc();
        try{
            for(GroupAccount groupAccount:groupAccounts){
                groupAccountService.resetGroupScoreByGid(groupAccount.getGid());
            }
            return "success";
        }catch (Exception e){
            e.printStackTrace();
            return "error";
        }
    }
}
